﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Globalization;
using System.Windows;
using WPF_GUI.AirportSVC;
using WPF_GUI.ViewModels;
using System.Windows.Media;

namespace WPF_GUI.Converters
{
    // Class to format values for display in GUI
    public class RunwayFormatter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            switch ((int)value)
            {
                case MainPageViewModel._runwayId24L:
                    return "Runway 24L";
                case MainPageViewModel._runwayId24R:
                    return "Runway 24R";
                case MainPageViewModel._runwayId25L:
                    return "Runway 25L";
                case MainPageViewModel._runwayId25R:
                    return "Runway 25R";
                case MainPageViewModel._runwayId6R:
                    return "Runway 6R";
                case MainPageViewModel._runwayId6L:
                    return "Runway 6L";
                case MainPageViewModel._runwayId7R:
                    return "Runway 7R";
                case MainPageViewModel._runwayId7L:
                    return "Runway 7L";
                default:
                    return "No Endpoint Selected";
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    // Convert Node to a string consisting of XPos and YPos for tooltip
    public class NodeFormatter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Node input = (Node)value;

            return "(" + input.XPos + ", " + input.YPos + ")";
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class PlaneFormatter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Plane plane = (Plane)value;

            if (plane != null)
            {
                // If the Plane has an associated Flight, return the FlightName
                if (plane.PlaneFlight != null)
                {
                    return plane.PlaneFlight.FlightName;
                }

                // Otherwise, return the Plane's Name
                return plane.Name;
            }
            else
            {
                return "";
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }    

    public class BooleanToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
                              CultureInfo culture)
        {
            if (value is Boolean)
            {
                return ((bool)value) ? Visibility.Visible : Visibility.Collapsed;
            }

            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter,
                                  CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class FloatToIntConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
                              CultureInfo culture)
        {
            float floatValue = (float)value;
            int intValue = (int)floatValue;

            return intValue;
        }

        public object ConvertBack(object value, Type targetType, object parameter,
                                  CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class HeadingToPlaneIconRotation : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
                              CultureInfo culture)
        {
            int actualHeading = (int)value;

            //return actualHeading - 45;
            return actualHeading;
        }

        public object ConvertBack(object value, Type targetType, object parameter,
                                  CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class BooleanToPlaneIconPathConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
                              CultureInfo culture)
        {
            if (value is Boolean)
            {
                string unselectedPlaneIcon = "/WPF_GUI;component/Images/air_plane_white_64x64.png";
                string selectedPlaneIcon = "/WPF_GUI;component/Images/air_plane_highlight_64x64.png";

                return ((bool)value) ? selectedPlaneIcon : unselectedPlaneIcon;
            } 

            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter,
                                  CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class ClockFormatter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
                              CultureInfo culture)
        {
            Clock clock = (Clock)value;

            return clock.Hours + ":" + clock.Minutes + ":" + clock.Seconds;
        }

        public object ConvertBack(object value, Type targetType, object parameter,
                                  CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class DateTimeFormatter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
                              CultureInfo culture)
        {
            DateTime time = (DateTime)value;

            string hoursString = "";
            string minutesString = "";
            string secondsString = "";

            if (time.Hour < 10)
            {
                hoursString = "0";
            }
            hoursString += time.Hour;

            if (time.Minute < 10)
            {
                minutesString = "0";
            }
            minutesString += time.Minute;

            if (time.Second < 10)
            {
                secondsString = "0";
            }
            secondsString += time.Second;

            return hoursString + ":" + minutesString + ":" + secondsString;
        }

        public object ConvertBack(object value, Type targetType, object parameter,
                                  CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class ScenarioFormatter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return -1;
            }

            Scenario input = (Scenario)value;

            return input.ScenarioId;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class ArrowConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Plane plane = (Plane)value;

            if (plane != null)
            {
                // If the Plane has an associated Flight, return whether the flight is a departure
                if (plane.PlaneFlight != null)
                {
                    if (!plane.PlaneFlight.IsDeparture)
                    {
                        return "/WPF_GUI;component/Images/arrow_white_down.png";
                    }
                }
            }

            return "/WPF_GUI;component/Images/arrow_white_up.png";
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class ColorStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value.ToString();
        }
    }

    public class PlanesInvolvedConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            List<Plane> planesInvolved = (List<Plane>)value;
            string planesInvolvedString = "";

            for (int i = 0; i < planesInvolved.Count; i++)
            {
                planesInvolvedString += planesInvolved[i].Name;

                if (i < planesInvolved.Count - 1)
                {
                    planesInvolvedString += ", ";
                }
            }

            return planesInvolvedString;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value.ToString();
        }
    }

    public class VectorMagnitudeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            WorldVector velocity = (WorldVector)value;
            float x = velocity.X;
            float y = velocity.Y;
            float z = velocity.Z;

            double magnitude = Math.Sqrt((x * x) + (y * y) + (z * z));
            magnitude *= 5.174;

            return magnitude;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value.ToString();
        }
    }
}
